<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/1/27
 * Time: 21:56
 */

namespace app\admin\controller;


use app\common\controller\AdminBase;
use think\Db;
use think\Request;

class Robot extends AdminBase
{
    protected $robot_model;

    public function __construct(Request $request = null)
    {

        $this->robot_model = new \app\common\model\Robot();

        parent::__construct($request);
    }

    public function index($keyword = '', $page = 1)
    {
        $map = [];
        if ($keyword) {
            $map['username|mobile|email|user_nick'] = ['like', "%{$keyword}%"];
        }

        $is = Db::name('system')->where(['name'=> 'robot_enable'])->find();
        $user_list = $this->robot_model->alias('r')->join('__ROBOT_ROOM__ rr','rr.robot_id = r.id','left')->where($map)->order('id DESC,room_uuid desc')->paginate(15, false, ['page' => $page]);


        return $this->fetch('index', ['keyword' => $keyword, 'user_list' => $user_list,'robot_status' => $is]);
    }


    public function edit($id = 0)
    {

        $user_info = $this->robot_model->find($id);
        $this->assign('user', $user_info);
        return $this->fetch();

    }


    public function save()
    {

        if ($this->request->isPost()) {
            $data = $this->request->post();
            if ($this->robot_model->allowField(true)->save($data)) {
                $this->success('保存成功');
            } else {
                $this->error('保存失败');
            }
        }

    }

    public function update($id)
    {
        if ($this->request->isPost()) {
            $data = $this->request->post();
            $validate_result = $this->validate($data, 'User');

            if ($validate_result !== true) {
                $this->error($validate_result);
            } else {
                $user = $this->robot_model->find($id);
                $user->id = $id;
                $user->username = $data['username'];
                $user->status = $data['status'];
                $user->headimg = $data['headimg'];
                if ($user->save() !== false) {
                    $this->success('更新成功');
                } else {
                    $this->error('更新失败');
                }
            }
        }

    }

    public function add()
    {
        return $this->fetch();
    }

    public function upload()
    {
        $file = request()->file('avatar');

        // 移动到框架应用根目录/public/uploads/ 目录下
        if ($file) {
            $info = $file->move(ROOT_PATH . 'public' . DS . 'uploads');
            if ($info) {
                $url = DS . 'public' . DS . 'uploads';
                $path = $url . DS . $info->getSaveName();

                $path = str_replace("\\", "/", $path);

                $return_data = [
                    'code' => 0,
                    'msg' => '上传成功',
                    'data' => ['url' => $path]
                ];
                echo json_encode($return_data);
                exit();

            } else {
                $return_data = [
                    'code' => 100,
                    'msg' => $file->getError(),

                ];
                echo json_encode($return_data);
                exit();
            }
        }
    }

    /**
     * 删除用户
     * @param $id
     */
    public function delete($id)
    {

        $info = Db::name('robot_room')->where(['robot_id' => $id])->find();
        if(!empty($info)){
            $this->error('该机器人已关联房间，无法删除');
        }
        if ($this->robot_model->destroy($id)) {
            $this->success('删除成功');
        } else {
            $this->error('删除失败');
        }
    }

    public function changeRobotStatus(){
        $state = $this->request->post('state');
        $status  = '1';
        if($state == 'false'){
            $status = '0';
        }
        $rs = Db::name('system')->where(['name' => 'robot_enable'])->update(['value' => $status]);
        if($rs){
            $res =refreshCache('robot_enable');
            $this->success('修改成功'.$res);
        }
        $this->error('修改失败');
    }
}